package com.wish.housing.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.housing.entity.HouseInfo;
import com.wish.housing.entity.RoomInfo;
import com.wish.housing.entity.vo.RoomRackVo;
import com.wish.housing.service.IHouseInfoService;
import com.wish.housing.service.IRoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 王泓翔-房间详细信息表	！房态-杨子强 前端控制器
 * </p>
 *
 * @author benjamin
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/room-info")
public class RoomInfoController {

    @Autowired
    IRoomInfoService iRoomInfoService;

    @GetMapping("/roomList")
    public MsgResult roomList(RoomRackVo qv) {
        PageHelper.startPage(qv.getPageNum(), qv.getPageSize());
        List<RoomRackVo> list = iRoomInfoService.roomRackList(qv);
        for (RoomRackVo roomRackVo : list) {
            String houseId = roomRackVo.getHouseId();
            List<RoomInfo> rooms = iRoomInfoService.getRoomList(houseId, qv);
            roomRackVo.setRoomInfoList(rooms);
            roomRackVo.setRoomNum(rooms.size());
        }
        PageInfo<RoomRackVo> pg = new PageInfo<>(list);
        return MsgResult.succeed(pg);
    }

    @Autowired
    IHouseInfoService iHouseInfoService;

    //    @RequestMapping(method = RequestMethod.DELETE)
    @DeleteMapping("/deleteHouseById/{houseId}")
    public MsgResult deleteHouseById(@PathVariable String houseId) {
        iHouseInfoService.removeById(houseId);
        return MsgResult.succeed();
    }

    @PutMapping("/updatePublishStatus")
    public MsgResult updatePublishStatus(@RequestBody HouseInfo houseInfo) {
        iHouseInfoService.updatePublishStatus(houseInfo.getId(), houseInfo.getPublishStatus());
        return MsgResult.succeed();
    }

    @PutMapping("/updateRental")
    public MsgResult updateRental(@RequestBody RoomInfo roomInfo) {
//        int x = iRoomInfoService.updateRental(roomInfo.getId(), roomInfo.getRental());
        boolean b = iRoomInfoService.saveOrUpdate(roomInfo);
        return MsgResult.succeed();
    }

}
